package com.safewind.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.safewind.dao.HistoryAddressDao;
import com.safewind.model.HistoryAddress;
import com.safewind.service.IHistoryAddressService;
import com.safewind.util.StringUtil;

@Service("historyAddressService")
public class HistoryAddressServiceImpl implements IHistoryAddressService {

	@Resource
	private HistoryAddressDao historyAddressDao;

	@Override
	public void importAddress(String userId, int length) {
		StringBuffer sb = new StringBuffer();
		sb.append("导入通讯录：");
		sb.append(length);
		sb.append("条");
		addHistoryAddress(userId, sb);
	}

	@Override
	public void addAddress(String userId, String name, String phoneNumber) {
		StringBuffer sb = new StringBuffer();
		sb.append("新增通讯录：姓名：");
		sb.append(name);
		sb.append("，号码：");
		sb.append(phoneNumber);
		addHistoryAddress(userId, sb);

	}

	@Override
	public void deleteAddress(String userId, int groupLength, int addressLength) {
		StringBuffer sb = new StringBuffer();
		sb.append("删除");
		if (groupLength != 0) {
			sb.append(groupLength);
			sb.append("个分组，");
		}
		sb.append(addressLength);
		sb.append("条通讯录");
		addHistoryAddress(userId, sb);
	}

	@Override
	public void deleteAll(String userId) {
		StringBuffer sb = new StringBuffer();
		sb.append("删除全部通讯录");
		addHistoryAddress(userId, sb);

	}

	@Override
	public void moveAddress(String userId, int length, String groupName) {
		StringBuffer sb = new StringBuffer();
		sb.append("将");
		sb.append(length);
		sb.append("条通讯录移动至 ");
		sb.append(groupName);
		addHistoryAddress(userId, sb);

	}

	@Override
	public void addGroup(String userId, String groupName) {
		StringBuffer sb = new StringBuffer();
		sb.append("新增分组：");
		sb.append(groupName);
		addHistoryAddress(userId, sb);

	}

	private void addHistoryAddress(String userId, StringBuffer sb) {
		HistoryAddress ha = new HistoryAddress();
		ha.setPkId(StringUtil.getUUID());
		ha.setContent(sb.toString());
		ha.setUserId(userId);
		ha.setCreateTime(new Date());
		historyAddressDao.insert(ha);
	}

	@Override
	public List<HistoryAddress> getAll(String userId) {
		return historyAddressDao.getAll(userId);
	}
}
